package com.shawn.datastructure.queue;

import java.util.Deque;
import java.util.LinkedList;

public class SlidingWindowMaximum {

	public int[] maxSlidingWindow(int[] nums, int k) {
		if (nums.length <= 1) {
			return nums;
		}
		Deque<Integer> q = new LinkedList();
		int n = nums.length;
		int[] res = new int[n - k + 1];
		for (int i = 0; i < k; i++) {
			while (!q.isEmpty() && nums[i] > nums[q.peekLast()]) {
				q.removeLast();
			}
			q.addLast(i);
		}
		int idx = 0;
		for (int j = k; j < n; j++) {
			res[idx++] = nums[q.peek()];
			while (!q.isEmpty() && q.peek() <= j - k) {
				q.removeFirst();
			}
			while (!q.isEmpty() && nums[j] > nums[q.peekLast()]) {
				q.removeLast();
			}
			q.addLast(j);
		}
		res[idx] = nums[q.peek()];
		return res;
	}

	/*
			if (a == null || k <= 0) {
			return new int[0];
		}
		int n = a.length;
		int[] r = new int[n-k+1];
		int ri = 0;
		// store index
		Deque<Integer> q = new ArrayDeque<>();
		for (int i = 0; i < a.length; i++) {
			// remove numbers out of range k
			while (!q.isEmpty() && q.peek() < i - k + 1) {
				q.poll();
			}
			// remove smaller numbers in k range as they are useless
			while (!q.isEmpty() && a[q.peekLast()] < a[i]) {
				q.pollLast();
			}
			// q contains index... r contains content
			q.offer(i);
			if (i >= k - 1) {
				r[ri++] = a[q.peek()];
			}
		}
		return r;
	 */

	/*
	 * 堆
	 *     int len = nums.length;
    int[] result = new int[len - k + 1];
    if(nums.length == 0) return new int[0];
    Queue<Integer> queue = new PriorityQueue<Integer>(new Comparator<Integer>(){
        @Override
        public int compare(Integer i1, Integer i2){
            return Integer.compare(i2, i1);
        }
    });

    for(int i = 0; i < k; i ++){
        queue.add(nums[i]);
    }
    result[0] = queue.peek();
    for(int i = k; i < len; i ++){
        queue.remove(nums[i - k]);
        queue.add(nums[i]);
        result[i - k + 1] = queue.peek();
    }

    return result;
	 */

	/*
	Deque Method: use deque to record the index, remove elements out of window in the head, and remove the smaller elements in the tail which have no change to become max.
    // O(n)
    public int[] maxSlidingWindow_deque(int[] nums, int k) {
        if(nums==null || k>nums.length || k<0) return null;
        if(k==0 || nums.length==0) return new int[0];
        Deque<Integer> dq = new LinkedList<>();
        int[] res = new int[nums.length-k+1];
        int index = 0; // index in r is equal to the left side of window
        for(int i=0; i<nums.length; i++){
            // remove elements out of window
            index = i-k+1;
            while(!dq.isEmpty() && dq.peek()<index){
                dq.poll();
            }
            // remove smaller elements, which have no change to become max
            while(!dq.isEmpty() && nums[dq.peek()]<nums[i]){
                dq.pollLast();
            }
            dq.offer(i);
            if(index>=0){
                res[index] = nums[dq.peek()];
            }
        }
        return res;
    }
Max Heap Method: a naive method. Build a max heap for a window, and remove the element out of window.
    //O((n-k)(lgk+k)) = O((n-k)k)
    public int[] maxSlidingWindow_heap1(int[] nums, int k) {
        if(nums==null || k>nums.length || k<0) return null;
        if(k==0 || nums.length==0) return new int[0];
        PriorityQueue<Integer> heap = new PriorityQueue<>( k, Collections.reverseOrder() );
        int[] res = new int[nums.length - k + 1];
        for(int i=0; i<k; i++)
            heap.offer(nums[i]);
        for(int p=0; p<res.length; p++){
            res[p] = heap.peek();
            heap.remove(nums[p]);
            if(p+k < nums.length)
                heap.offer(nums[p+k]);
        }
        return res;
    }
Max Heap Method 2: Using a class Tuple to record index info.
    // worst case O((n-k)logn) increasing order, best case O((n-k)logk) decreasing order
    public int[] maxSlidingWindow_heap2(int[] nums, int k) {
        if(nums==null || k>nums.length || k<0) return null;
        if(k==0 || nums.length==0) return new int[0];
        PriorityQueue<Tuple> heap = new PriorityQueue<>( k, Collections.reverseOrder() );
        int[] res = new int[nums.length - k + 1];
        for(int i=0; i<k; i++)
            heap.offer(new Tuple(nums[i], i));
        Tuple tup = null;
        for(int p=0; p<res.length; p++){
            tup = heap.peek();
            while(tup.idx<p) {
                heap.poll();
                tup = heap.peek();
            }
            res[p] = tup.val;
            if(p+k < nums.length)
                heap.offer(new Tuple(nums[p+k], p+k));
        }
        return res;
    }
    class Tuple implements Comparable<Tuple>{
        int val;
        int idx;
        public Tuple(int value, int index){
            val = value;
            idx = index;
        }
        @Override
        public int compareTo(Tuple tup){
            return (val - tup.val);
        }
    }

I came up with the second one first, then the third one. Then I looked at the discuss, and found people used deque with O(n). I did the first one.
	 */

}
